#  QDesktopWidget
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QDesktopWidget
#  PyQt5包  QtWidgets模块 QApplication类

class CenterForm(QMainWindow):
    def __init__(self, parent=None):
        super(CenterForm, self).__init__(parent)

        #  设置主窗口的标题
        self.setWindowTitle('让窗口居中')
        #  设置窗口的尺寸
        self.resize(800, 600)

    def center(self):
        #  获取屏幕坐标系
        screen = QDesktopWidget().screenGeometry()
        #  获取窗口坐标系
        size = self.geometry()
        new_left = int((screen.width() - size.width()) / 2)
        new_top = int((screen.height() - size.height()) / 2)
        self.move(new_left, new_top)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = CenterForm()
    main.center()
    main.show()
    sys.exit(app.exec_())
